/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yanlight.Constants;
import com.yanlight.app.dal.dao.ShopMemberDAO;
import com.yanlight.app.dal.entity.ShopMember;
import com.yanlight.app.request.ShopMembeRegisterRequest;
import com.yanlight.app.service.ShopMemberService;
import com.yanlight.exception.ErrorType;
import com.yanlight.exception.TradeException;
import com.yanlight.type.StatusType;
import com.yanlight.util.DateTimeUtil;
import com.yanlight.util.EncryptUtil;
import com.yanlight.util.ToolsUtil;

/**
 * 
 * @author haolw
 * @time 2018/01/04
 */
@Service
public class ShopMemberServiceImpl implements ShopMemberService {
	@Autowired
	private ShopMemberDAO shopMemberDAO;

	@Override
	public void register(ShopMembeRegisterRequest request) {
		String phone = request.getPhone();
		String password = request.getPassword();
		ShopMember shopMember = shopMemberDAO.findByPhone(phone);
		if (shopMember != null) {
			throw TradeException.error(ErrorType.REGISTERED_PHONE);
		}
		if (StringUtils.isBlank(password)) {
			password = Constants.INITIALIZE_PASSWORD;
		}
		password = EncryptUtil.md5AndSalt(password);
		shopMember = new ShopMember();
		shopMember.setPhone(phone);
		shopMember.setPassword(password);
		shopMember.setWxOpenid(request.getWxOpenid());
		shopMember.setAlipayUserid(request.getAlipayUserid());
		shopMember.setName(ToolsUtil.createMemberName(phone));
		shopMember.setStatus(StatusType.ACTIVITY.ordinal());
		shopMember.setCreateTime(DateTimeUtil.getNowTime());
		shopMemberDAO.save(shopMember);
	}
}
